package com.smart_campus.campus.controller;

import com.smart_campus.campus.domain.CampusJournalism;
import com.smart_campus.campus.service.CampusCarouselService;
import com.smart_campus.campus.service.CampusJournalismService;
import com.smart_campus.campus.vo.CampusJournalismVo;
import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.Feedback;
import com.smart_campus.sys.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Alfred
 * @time: 2020/6/28 19:27
 */
@RestController
@RequestMapping("journalism")
public class CampusJournalismController {

    @Autowired
    private CampusJournalismService campusJournalismService;

    /**
     * 查询学校新闻
     * @return campusJournalismVo
     */
    @RequestMapping("queryJournalism")
    public DataGridView queryJournalism(CampusJournalismVo campusJournalismVo){
        return this.campusJournalismService.queryJournalism(campusJournalismVo);
    }

    /**
     * 添加新闻
     * @param campusJournalism
     * @return
     */
    @RequestMapping("addJournalism")
    public ResultObj addJournalism(CampusJournalism campusJournalism){
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            User user = activeUser.getUser();
            campusJournalism.setUserId(user.getUserId());
            campusJournalism.setCreateTime(new Date());
            Integer integer1 = this.campusJournalismService.queryMaxId();
            campusJournalism.setId(integer1+1);
            Integer integer = this.campusJournalismService.queryMax();
            campusJournalism.setOrdernum(integer+1);
            this.campusJournalismService.insert(campusJournalism);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除新闻
     * @param id
     * @return
     */
    @RequestMapping("deleteJournalism")
    public ResultObj deleteJournalism(Integer id){
        try {
            this.campusJournalismService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    /**
     * 批量删除意见
     * @param ids
     * @return
     */
    @RequestMapping("deleteBatchJournalism")
    public ResultObj deleteBatchJournalism(Integer ids[]){
        try {
            for (Integer id: ids) {
                this.campusJournalismService.deleteByPrimaryKey(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改
     * @param campusJournalism
     * @return
     */
    @RequestMapping("updateJournalism")
    public ResultObj updateJournalism(CampusJournalism campusJournalism){
        try {
            this.campusJournalismService.updateByPrimaryKeySelective(campusJournalism);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 查询单个
     * @param campusJournalism
     * @return
     */
    @RequestMapping("selectJournalism")
    public CampusJournalism selectJournalism(CampusJournalism campusJournalism){
        try {
            campusJournalism=this.campusJournalismService.selectByPrimaryKey(campusJournalism.getId());
            return campusJournalism;
        } catch (Exception e) {
            e.printStackTrace();
            return campusJournalism;
        }
    }
}
